package cn.ibdsr.human.controller;


import cn.ibdsr.human.constant.BizConstants;
import cn.ibdsr.human.constant.ErrorTypeEnum;
import cn.ibdsr.human.constant.Result;
import cn.ibdsr.human.constant.ServerInitEnum;
import cn.ibdsr.human.db.model.ServerInit;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.manager.DataFormatManager;

import cn.ibdsr.human.service.DataCenterService;
import cn.ibdsr.human.service.ImageService;
import cn.ibdsr.human.transfer.ServerInitDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 文件操作接口
 *
 * @author chenglx
 * @version V1.0
 * @date 2021/3/9 13:51
 * <p>
 * Date Author Description
 * ------------------------------------------------------
 * 2021/1/5 chenglx init
 */
@Api(tags = "图片处理控制器")
@RestController
@Slf4j
@Validated
@RequestMapping("/images")
public class ImageController {
    @Resource
    private ImageService imageService;
    @Resource
    private DataCenterService dataCenterService;
    @Autowired
    private DataFormatManager formatManager;
    /**
     * 上传文件（包括图片和文件）
     * 文件最大不能超过 500M
     * 注：请求首部中需加上 "Content-Type: multipart/form-data"
     *
     * @param file 上传的文件
     * @return
     */
    @PostMapping
    public Object upload(@RequestParam("file") MultipartFile file) {
        if (file == null) {
            throw new BusinessException(BizExceptionEnum.IMAGE_FILE_CONTENT_ERROR);
        }
        long size = file.getSize();
        if (size > BizConstants.MAX_FILE_SIZE) {
            log.warn("upload(): 图片尺寸大于 {} B，上传失败：size={}", BizConstants.MAX_FILE_SIZE, size);
            throw new BusinessException(BizExceptionEnum.IMAGE_FILE_SIZE_ERROR);
        }
        String name = FilenameUtils.getExtension(file.getOriginalFilename());
        String path = this.imageService.upload(file);
        path = formatManager.complementFileUrl(path);
        return new Result<>(path);
    }

    /**
     * 删除文件
     *
     * @param filePath 完整的文件路径
     * @return
     */
    @DeleteMapping
    public Object delete(@RequestParam("filePath") String filePath) {
        if (StringUtils.isEmpty(filePath)) {
            throw new BusinessException(BizExceptionEnum.IMAGE_FILE_CONTENT_ERROR);
        }
        try {
            imageService.delete(filePath);
        } catch (Exception e) {
            log.error("delete(): 删除文件失败：filePath={}", filePath, e);
            return new Result<>(ErrorTypeEnum.SERVER_ERROR.getHttpStatusCode(), e.getMessage());
        }
        // 返回删除的文件数1
        return new Result<>(true);
    }

    /**
     * cdl-ok
     * 获取进度
     */
    @ApiOperation("数据库备份")
    @GetMapping("/backup")
    public void backup( ) {
        dataCenterService.backup();
    }
}